
export default class MixinUtil {
  /*
    构造mixin对象
    data：原始数据
    mixin：织入对象
 */
  data = {}
  mixin = {}
  methods = {}
  static makeConstants(name, data, methods) {
    return new MixinUtil(name, data, methods)
  }
  constructor(name, data, methods) {
    const dataCp = JSON.parse(JSON.stringify(data))
    let i = 0
    for (const key in dataCp) {
      dataCp[key].index = i++
    }
    this.data = dataCp
    const mixinObj = {
      ...dataCp
    }
    // Object.defineProperty(mixinObj, 'getValueByKey', { value: this.getValueByKey, enumerable: false })
    this.mixin = {
      data() {
        return {
          [name]: {
            ...mixinObj
          }
        }
      }
    }
    this.methods = methods
    return this
  }
  getValueByKey(pKey, keyNode = 'key', valueNode = 'value') {
    const obj = this.data
    for (const key in obj) {
      if (pKey === obj[key][keyNode]) {
        return obj[key][valueNode]
      }
    }
    return null
  }
  getKeys(keyNode = 'key') {
    const obj = this.data
    const kArr = []
    for (const key in obj) {
      kArr.push(obj[key][keyNode])
    }
    return kArr
  }
  getOptions() {
    const obj = this.data
    const kArr = []
    for (const key in obj) {
      kArr.push(key)
    }
    return kArr
  }
  getValues(valueNode = 'value') {
    const obj = this.data
    const vArr = []
    for (const key in obj) {
      vArr.push(obj[key][valueNode])
    }
    return vArr
  }
}
